﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Hotel_application.Models;
using System.IO;

namespace Hotel_application.Controllers
{
    public class ReceptionController : Controller
    {
        //
        // GET: /Reception/
        
        public ActionResult NhanPhong()
        {
            var ks = new KhachSanEntities();
            return View(ks);
        }


        [HttpGet]
        public ActionResult Search_Reservation()
        {
            var ks = new KhachSanEntities();
            var date =  Convert.ToDateTime(Request.QueryString["timeIn"]);
            var hoTen = Request.QueryString["hoTen"];
            var res = ks.proc_TimKiemDatPhongCaNhan(date,hoTen);
            return PartialView(res);
        }


        [HttpGet]
        public ActionResult Search_Reservation_Company()
        {
            var ks = new KhachSanEntities();
            var date = Convert.ToDateTime(Request.QueryString["timeIn"]);
            var hoTen = Request.QueryString["hoTen"];
            var res = ks.proc_TimKiemDatPhongCongTy(date, hoTen);
            return PartialView(res);
        }


        [HttpGet]
        public ActionResult Update_Customer()
        {
            //try
            //{
            var ks = new KhachSanEntities();
            string maDS = Request.QueryString["maDS"];
            string CMND_old = Request.QueryString["CMND_old"];
            ThongTinNguoiO lp = ks.ThongTinNguoiOs.First(i => i.maDS == maDS && i.CMND == CMND_old);
            ks.ThongTinNguoiOs.Remove(lp);
            ks.SaveChanges();

            ThongTinNguoiO no = new ThongTinNguoiO();
            no.tenNguoiO = Request.QueryString["hoTen"];
            no.CMND = Request.QueryString["CMND"];
            no.maDS = maDS;
            ks.ThongTinNguoiOs.Add(no);
            //lp.img_name = Request.QueryString["img_name" + id];
            ks.SaveChanges();
            return Content("Cập nhật thành công");
            //}
            //catch (Exception ex)
            //{
            //    return Content(Convert.ToString(ex.InnerException));
            //}

        }


        [HttpGet]
        public ActionResult Update_Service()
        {
            //try
            //{
            var ks = new KhachSanEntities();
            string maDS = Request.QueryString["maDS"];
            string maDV = Request.QueryString["maDV"];
            SuDungDichVu lp = ks.SuDungDichVus.First(i => i.maDS == maDS && i.maDV == maDV);
            lp.soLuong = Convert.ToInt16(Request.QueryString["soLuong"]);
            //lp.img_name = Request.QueryString["img_name" + id];
            ks.SaveChanges();
            return Content("Cập nhật thành công");
            //}
            //catch (Exception ex)
            //{
            //    return Content(Convert.ToString(ex.InnerException));
            //}

        }


         [HttpGet]
        public ActionResult Add_Service()
        {
            //try
            //{
            //lay ma loai
            var ks = new KhachSanEntities();
            SuDungDichVu lp = new SuDungDichVu();
            var maDS = Request.QueryString["maDS"];
            var maDV = Request.QueryString["maDV"];
            var soLuong = Request.QueryString["soLuong"];
            lp.maDS = maDS;
            lp.maDV = maDV;
            lp.soLuong = Convert.ToInt16(soLuong);
            ks.SuDungDichVus.Add(lp);
            //lp.img_name = Request.QueryString["img_name" + id];
            ks.SaveChanges();
            DichVu dv = ks.DichVus.First(i => i.maDV == maDV);
            
            var content = "<div id='"+maDS+maDV+"'>";
            content += "<label>" + dv.tenDV + "</label>";
            content += "<input id='maDV_"+maDS+"' type='hidden' value='"+maDV+"'/>";
            content += "<input type='text' value='"+ soLuong +"' id='soLuong_"+ maDS +maDV+"' />";
            content += "<p><a id='_update_" + maDS+maDV + "' class='update_service btnInner'>Cập nhật</a>";
            content += "<a id='_delete_" + maDS+maDV + "' name='btn_delete' type='button' value='Xóa dịch vụ này' class='delete_service btnInner'>Xoá</a></p></div>";
            return Content(content);
            //}
            //catch (Exception ex)
            //{
            //    return Content(Convert.ToString(ex.InnerException));
            //}

        }



         [HttpGet]
         public ActionResult Delete_Service()
         {
             //try
             //{
             var ks = new KhachSanEntities();
             var maDS = Request.QueryString["maDS"];
             var maDV = Request.QueryString["maDV"];
             SuDungDichVu lp = ks.SuDungDichVus.First(i => i.maDV == maDV && i.maDS == maDS);
             ks.SuDungDichVus.Remove(lp);
             ks.SaveChanges();
             return Content("Đã xóa dịch vụ");
             //}
             //catch (Exception ex)
             //{
             //    return Content(Convert.ToString(ex.InnerException));
             //}

         }



         [HttpGet]
         public ActionResult Add_Customer()
         {
             //try
             //{
             //lay ma loai
             var ks = new KhachSanEntities();
             ThongTinNguoiO lp = new ThongTinNguoiO();
             var maDS = Request.QueryString["maDS"];
             var hoTen = Request.QueryString["hoTen"];
             var CMND = Request.QueryString["CMND"];
             lp.CMND = CMND;
             lp.maDS = maDS;
             lp.tenNguoiO = hoTen;
             ks.ThongTinNguoiOs.Add(lp);
             //lp.img_name = Request.QueryString["img_name" + id];
             ks.SaveChanges();
             var content = "<div id='" + maDS + CMND + "'>";
             content += "<p><label>Họ tên</label>";
             content += "<input type='text' value='"+ hoTen +"' id='hoTen_"+maDS+CMND+"' /></p>";
             content += "<p><label>CMND</label>";
             content += "<input type='text' value='"+CMND+"' id='cmnd_"+maDS+CMND+"' /></p>";
             content += "<input type='hidden' value='"+CMND+"' id='cmnd_old_"+maDS+CMND+"' />";
             content += "<p><a id='update_"+maDS+CMND+"' class='update_customer btnInner'>Cập nhật</a>";
             content += "<a id='delete_"+maDS+CMND+"' class='delete_customer btnInner'>Xoá</a></p></div>";
             return Content(content);
             //}
             //catch (Exception ex)
             //{
             //    return Content(Convert.ToString(ex.InnerException));
             //}

         }



         [HttpGet]
         public ActionResult Delete_Customer()
         {
             //try
             //{
             var ks = new KhachSanEntities();
             var maDS = Request.QueryString["maDS"];
             var CMND = Request.QueryString["CMND"];
             ThongTinNguoiO lp = ks.ThongTinNguoiOs.First(i => i.CMND== CMND && i.maDS == maDS);
             ks.ThongTinNguoiOs.Remove(lp);
             ks.SaveChanges();
             return Content("Đã xóa khách hàng");
             //}
             //catch (Exception ex)
             //{
             //    return Content(Convert.ToString(ex.InnerException));
             //}

         }

         [HttpGet]
         public ActionResult CheckIn()
         {
             //try
             //{
             var ks = new KhachSanEntities();
             string maDP = Request.QueryString["maDP"];
             DatPhong lp = ks.DatPhongs.First(i => i.maDP == maDP);
             lp.maNV = "NV00000000";
             //lp.img_name = Request.QueryString["img_name" + id];
             ks.SaveChanges();
             return Content("Cập nhật thành công");
             //}
             //catch (Exception ex)
             //{
             //    return Content(Convert.ToString(ex.InnerException));
             //}

         }


         [HttpGet]
         public ActionResult CheckOut()
         {
             //try
             //{
             var ks = new KhachSanEntities();
             string maDP = Request.QueryString["maDP"];
             DatPhong lp = ks.DatPhongs.First(i => i.maDP == maDP);
             lp.xacNhanTraPhong = true;
             //lp.img_name = Request.QueryString["img_name" + id];
             ks.SaveChanges();
             return Content("Cập nhật thành công");
             //}
             //catch (Exception ex)
             //{
             //    return Content(Convert.ToString(ex.InnerException));
             //}

         }

    }
}
